package com.ssy.lingxi.member.merchant.controller.web;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.model.vo.basic.request.MemberIdVO;
import com.ssy.lingxi.member.merchant.model.vo.platform.request.*;
import com.ssy.lingxi.member.merchant.model.vo.platform.response.*;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberCacheService;
import com.ssy.lingxi.member.merchant.service.base.IBaseSiteService;
import com.ssy.lingxi.member.merchant.service.web.IPlatformMemberRoleRuleService;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author whm
 * @version 2.0.0
 * @menu 平台后台 - 设置会员适用角色规则相关接口
 * @date 2022-03-10
 **/
@RestController
@RequestMapping("/member/platform/roleRule")
public class PlatformRoleRuleManageController {
    @Resource
    private IPlatformMemberRoleRuleService roleRuleService;

    @Resource
    private IBaseMemberCacheService baseMemberCacheService;

    @Resource
    private IBaseSiteService baseSiteService;

    /**
     * 校验是否开启SAAS多租户部署
     *
     * @param headers Http头部信息
     * @return 查询结果
     */
    @RequestMapping(value = "/isEnableMultiTenancy", method = RequestMethod.GET)
    public Wrapper<Boolean> isEnableMultiTenancy(@RequestHeader HttpHeaders headers) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return baseSiteService.isEnableMultiTenancy(headers);
    }

    /**
     * 根据会员名称，模糊、分页查询会员列表
     *
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @RequestMapping(value = "/memberPage", method = RequestMethod.GET)
    public Wrapper<PageData<MemberManageVO>> pageMembers(@RequestHeader HttpHeaders headers, @Valid PageMemberByNameVO pageVO) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return roleRuleService.pageMembers(pageVO);
    }

    /**
     * 根据会员名称，模糊、分页查询会员管理列表
     *
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @RequestMapping(value = "/memberRulePage", method = RequestMethod.GET)
    public Wrapper<PageData<MemberRoleRuleVO>> pageRuleMembers(@RequestHeader HttpHeaders headers, @Valid PageMemberByNameVO pageVO) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return roleRuleService.pageRuleMembers(pageVO);
    }

    /**
     * 根据角色名称，模糊、分页查询角色详情列表
     *
     * @param headers Http头部信息
     * @return 查询结果
     */
    @RequestMapping(value = "/rolePage", method = RequestMethod.POST)
    public Wrapper<PageData<RoleRuleManageVO>> pageRoles(@RequestHeader HttpHeaders headers, @Valid @RequestBody PageRoleRuleVO pageVO) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return roleRuleService.pageRoles(pageVO);
    }

    /**
     * 查询下级会员适用角色详情列表(注册)
     *
     * @param headers Http头部信息
     * @return 查询结果
     */
    @RequestMapping(value = "/register/subMemberRole", method = RequestMethod.GET)
    public Wrapper<List<RoleRuleRegisterVO>> pageRoles(@RequestHeader HttpHeaders headers, @Valid MemberIdVO idVO) {
        baseMemberCacheService.checkWebRequestHeader(headers);
        Boolean enableMultiTenancy = baseSiteService.isEnableMultiTenancy(headers).getData();
        if (enableMultiTenancy) {
            return roleRuleService.subMemberRolesRegister(idVO.getMemberId());
        } else {
            return Wrapper.fail(ResponseCode.MC_MS_SY_SITE_NOT_ENABLED_MULTI_TENANCY);
        }
    }

    /**
     * 查询当前会员适用角色详情列表
     *
     * @param headers Http头部信息
     * @param idVO    接口参数
     * @return 查询结果
     */
    @RequestMapping(value = "/memberRole", method = RequestMethod.GET)
    public Wrapper<List<RoleRuleManageVO>> memberRoles(@RequestHeader HttpHeaders headers, @Valid MemberByIdVO idVO) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return roleRuleService.memberRoles(idVO.getMemberId());
    }

    /**
     * 查询下级会员适用角色详情列表
     *
     * @param headers Http头部信息
     * @param idVO    接口参数
     * @return 查询结果
     */
    @RequestMapping(value = "/subMemberRole", method = RequestMethod.GET)
    public Wrapper<List<RoleRuleManageVO>> subMemberRoles(@RequestHeader HttpHeaders headers, @Valid MemberByIdVO idVO) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return roleRuleService.subMemberRoles(idVO.getMemberId());
    }

    /**
     * “新增会员适用角色” - 选择会员
     *
     * @param headers    Http头部信息
     * @param memberIdVO 接口参数
     * @return 查询结果
     */
    @RequestMapping(value = "/selectMember", method = RequestMethod.GET)
    public Wrapper<MemberSelectVO> selectMember(@RequestHeader HttpHeaders headers, @Valid MemberIdVO memberIdVO) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return roleRuleService.selectMember(headers, memberIdVO);
    }

    /**
     * “新增会员适用角色” - 增加角色
     *
     * @param headers    Http头部信息
     * @param roleRuleVO 接口参数
     * @return 查询结果
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper<Void> addRuleRoles(@RequestHeader HttpHeaders headers, @Valid @RequestBody AddRoleRuleVO roleRuleVO) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return roleRuleService.addRuleRoles(headers, roleRuleVO);
    }

    /**
     * 删除会员适用角色
     *
     * @param headers    Http头部信息
     * @param roleRuleVO 接口参数
     * @return 查询结果
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper<Void> delRuleRoles(@RequestHeader HttpHeaders headers, @Valid @RequestBody DelRoleRuleVO roleRuleVO) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return roleRuleService.delRuleRoles(headers, roleRuleVO);
    }

    /**
     * 会员适用角色详情列表
     *
     * @param headers Http头部信息
     * @param idVO    接口参数
     * @return 查询结果
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Wrapper<MemberRoleRuleDetailVO> getDetails(@RequestHeader HttpHeaders headers, @Valid MemberDetailByIdVO idVO) {
        baseMemberCacheService.needLoginFromManagePlatform(headers);
        return roleRuleService.getDetails(idVO);
    }
}
